﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using ArcSoftFace.Utils;
using System.IO;
using Image = System.Drawing.Image;

namespace AllInOne.Pages
{
    /// <summary>
    /// SignPadPage.xaml 的交互逻辑
    /// </summary>
    /// 
    public partial class SignPadPage : Page
    {
        public MainWindow parent;

        public SignPadPage()
        {
            InitializeComponent();
        }

        public SignPadPage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        public void InitPage()
        {
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));
            this.signCanvas.Strokes.Clear();
            this.parent.PlaySound(Properties.Resources.male_input_sign, Properties.Resources.female_input_sign);

        }

        private void BtnClear_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.signCanvas.Strokes.Clear();
            
        }

        private void BtnSave_MouseUp(object sender, MouseButtonEventArgs e)
        {
            RenderTargetBitmap rtb = new RenderTargetBitmap(900,315,96,96, PixelFormats.Default);
            rtb.Render(this.signCanvas);

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            string fileName = "sign.png";
            using (var stream = new FileStream(fileName, FileMode.Create))
            {
                encoder.Save(stream);
            }
            Image signImage = Image.FromFile(fileName);
            string qmtp = ImageUtil.BitmapToString((Bitmap)signImage);

            string xm = this.parent.idInfo.getName().Replace("\0", "").Replace(" ", "");
            string sfzmhm = this.parent.idInfo.getId().Replace("\0", "").Replace(" ", "");

            //上传签名到服务器
            long qmId = ServiceApi.UploadTp(sfzmhm, xm, qmtp, "qm");

            this.parent.SetSignInfo(qmtp, qmId);

        }

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }
    }


}
